﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApp
{
    public class TestReflect
    {

        public A a;

        public TestReflect()
        {
            a = new A();
        }

        public static void Test()
        {
            //TestReflect testReflect = new TestReflect();

            //Console.WriteLine(testReflect.a.GetInstanceName());


            //mystruct mystruct1 = new mystruct();
            //mystruct1.a = 1;
            //mystruct1.b = 1;

            byte[] buffer = new byte[4];
            buffer[0] = 0;
            buffer[1] = 1;





         }


        public static T BytesToStruct<T>(byte[] bytes, int startIndex, int length)
        {
            if (bytes == null) return default(T);
            if (bytes.Length <= 0) return default(T);
            IntPtr buffer = Marshal.AllocHGlobal(length);
            try//struct_bytes转换
            {
                Marshal.Copy(bytes, startIndex, buffer, length);
                return (T)Marshal.PtrToStructure(buffer, typeof(T));
            }
            catch (Exception ex)
            {
                throw new Exception("Error in BytesToStruct ! " + ex.Message);
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
    }

    public class A
    {

        public string GetInstanceName([CallerMemberName] string propertyName = null)
        {
            string InstanceName = "";
            var type = this.GetType();
            InstanceName = this.ToString();

            return InstanceName;
        }
    }

}
